<template>
  <div class="search-suggestion">
    <van-cell
      v-for="(item, index) in list"
      :key="index"
      @click="$emit('search', item)"
      icon="search"
    >
      <div slot="title" v-html="reverseStr(item)"></div>
    </van-cell>
  </div>
</template>

<script>
import { getSuggestionData } from "@/api/search";
// import { lodash } from "lodash";
export default {
  name: "SearchSuggestion",
  components: {},
  props: {
    searchText: {
      type: String,
    },
  },
  data() {
    return {
      list: [],
    };
  },
  watch: {
    searchText: {
      async handler() {
        console.log(this.searchText);
        if (this.timer) clearInterval(this.timer);
        this.timer = setTimeout(async () => {
          let { data } = await getSuggestionData({
            q: this.searchText,
          });
          console.log(data);
          this.list = data.data.options;
        });
      },

      deep: false,
      immediate: true,
    },
  },
  methods: {
    /*   async loadSearchSuggestion(val) {
      let { data } = await getSuggestionData({
        q: val,
      });
      this.list = data.data.options;
    }, */
    reverseStr(val) {
      console.log(val);
      return val.replaceAll(
        this.searchText,
        `<span style="color: red">${this.searchText} </span>`
      );
    },
  },
};
</script>

<style scoped lang="less"></style>